package com.ruoyi.hr.service.impl.trainmanage;


import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.hr.mapper.trainmanage.TrainManageMapper;
import com.ruoyi.hr.service.trainmanage.TrainManageService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ExcelUtils;
import com.ruoyi.hr.util.ResultEnum;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class TrainManageServiceImpl extends BaseController implements TrainManageService {

    @Autowired
    TrainManageMapper trainManageMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findDataList(Map map) {
        return trainManageMapper.findDataList(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findDataListHistory(Map map) {
        return trainManageMapper.findDataListHistory(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int add(Map map) {
        return trainManageMapper.add(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public Map<String, Object> findDataById(String id) {
        return trainManageMapper.findDataById(id);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int edit(Map map) {
        return trainManageMapper.edit(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int remove(String ids) {
        String[] idSplit = ids.split(",");
        int num = 0;
        for (String id : idSplit) {
            //删除对应参加员工
            trainManageMapper.removeTrainEmp(id);
            //删除对应附件信息
            String fileIds = trainManageMapper.findFileIds(id);
            this.removeFile(fileIds);
            //删除培训数据
            num += trainManageMapper.remove(id);
        }
        return num;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findEmp(Map map) {
        return trainManageMapper.findEmp(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int addEmp(String trainId, String AllEmpEid) {
        int num = 0;
        String[] idSplit = AllEmpEid.split(",");
        for (String eid : idSplit) {
            num += trainManageMapper.addEmp(trainId, eid);
        }
        return num;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findTrainEmp(String id) {
        return trainManageMapper.findTrainEmp(id);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int removeTrainEmp(String trainId) {
        return trainManageMapper.removeTrainEmp(trainId);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public String findEmpNames(String trainId) {
        return trainManageMapper.findEmpNames(trainId);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findFileDataList(Map map) {
        return trainManageMapper.findFileDataList(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int addFile(Map map) {
        return trainManageMapper.addFile(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int removeFile(String ids) {
        int num = 0;
        if (ids != null) {
            //删除文件
            String[] idSplit = ids.split(",");
            for (String id : idSplit) {
                Map map = trainManageMapper.findFileById(id);
                //删除文件
                FileUtils.deleteFile(map.get("deletePath").toString());
                //删除数据
                num = trainManageMapper.removeFile(id);
            }
        }
        return num;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findEmpList(String trainId) {
        return trainManageMapper.findEmpList(trainId);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public AjaxResult exportData(String trainId) {
        Map map = new HashMap();
        map.put("trainId", trainId);

        List<Map<String, Object>> listMap = this.findEmpList(trainId);

        for (Map<String, Object> map1 : listMap) {
            //keySet获取map集合key的集合  然后在遍历key即可
            for (String key : map1.keySet()) {
                if (map1.get(key) == null) {
                    map1.put(key, "");
                }
            }
        }

        try {
            HSSFWorkbook wkb = new HSSFWorkbook();

            String sheetname = "培训分数导入模板";
            //设置Sheet名称
            HSSFSheet sheet = wkb.createSheet(sheetname);
            //新增一行(用于列名)
            HSSFRow row1 = sheet.createRow(0);
            //设置固定列名
            row1.createCell(0).setCellValue("序号");
            row1.createCell(1).setCellValue("姓名");
            row1.createCell(2).setCellValue("工号");
            row1.createCell(3).setCellValue("分数");

            //将数据放到Excel中
            HSSFRow row;
            for (int i = 1; i <= listMap.size(); i++) {
                //循环写入行
                row = sheet.createRow(i);
                row.createCell(0).setCellValue(listMap.get(i - 1).get("id").toString());
                row.createCell(1).setCellValue(listMap.get(i - 1).get("name").toString());
                row.createCell(2).setCellValue(listMap.get(i - 1).get("badge").toString());
                row.createCell(3).setCellValue(listMap.get(i - 1).get("score").toString());
            }
            String filePath = Global.getDownloadPath() + sheetname + ".xls";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wkb.write(fileOut);
            fileOut.close();
            return AjaxResult.success(0);
        } catch (Exception e) {
            return AjaxResult.error("失败");
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public AjaxResult importData(MultipartFile file, String trainId) {
        List<String[]> excelData = ExcelUtils.getExcelData(file);
        int num = 0;
        String id = "";
        String score = "";
        for (int i = 1; i < excelData.size(); i++) {
            for (int j = 3; j < excelData.get(i).length; j++) {
                id = excelData.get(i)[0];
                //取项目id
                score = excelData.get(i)[3];
                num += trainManageMapper.updateScore(id, score);
            }
        }
        return toAjax(num);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult isSeal(String trainId) {
        String isSeal = trainManageMapper.isSeal(trainId);
        if (isSeal == null || "".equals(isSeal)) {
            //同步数据
            Map<String, Object> map = trainManageMapper.findDataById(trainId);
            List<Map<String, Object>> empList = trainManageMapper.findEmpList(trainId);
            for (Map map1 : empList) {
                map.put("eid", map1.get("eid"));
                map.put("score", map1.get("score"));
                trainManageMapper.insertTraining(map);
                map.remove("eid");
                map.remove("score");
            }
            //将封存状态改为1
            trainManageMapper.updateSeal(trainId);
            //未封存
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            //已封存
            return BaseResult.build(ResultEnum.FAILURE);
        }
    }
}
